package com.test.gpa.rest;

import com.test.gpa.domain.*;
import com.test.gpa.repostitory.*;
import com.test.gpa.security.Impl.LoginServiceImpl;
import com.test.gpa.security.config.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
public class rest {
    @Autowired
    bookRepository bookRepository;
    @Autowired
    dormRepository dormRepository;
    @Autowired
    StuRepository stuRepository;
    @Autowired
    SqlRepository sqlRepository;
    @Autowired
    UserRepository userRepository;

    @GetMapping("/sout")
    public void sout() {

//        Student s = new Student();
//        Set<Book> bookSet = new HashSet<>();
//        stuRepository.findAll();
//        s.setBooks(bookSet);
//      stuRepository.save();
         userRepository.findAll().forEach(user -> System.out.println(user));

        System.out.println();
    }

    @PostMapping("/query")
    public void noTest(@RequestBody Book book) {
        if (Objects.isNull(book.getBookName())) {
            System.out.println("book is null");
            return;
        }
        System.out.println(book.getBookName());
        List<Book> books = sqlRepository.queryBookById(book.getBookName());
        books.forEach(book1 -> {

            System.out.println(book1.getId());
        });

    }

    @PostMapping("/save")
    public Long save(@RequestBody BookAndStudent bookAndStudent) {
//        System.out.println(dorm.getDormName());
//        Dorm dorm1=dormRepository.save(dorm);
        System.out.println(bookAndStudent.getStudent().getClassName());
        Student student = stuRepository.save(bookAndStudent.getStudent());

        return student.getId();
    }

    @PostMapping("/saveDorm")
    public Long saveDorm(@RequestBody Dorm dorm) {
        System.out.println(dorm.getDormName());
        Dorm dorm1 = dormRepository.save(dorm);//有list的地方可以一起插入数据 联级更新
        return dorm1.getId();
    }

    @GetMapping("/queryBook")
    public void queryBook(int page, int size) {
        System.out.println(page + size);
        int start = (page - 1) * size;
        int end = page * size;
        sqlRepository.queryAllByPage(start, end).forEach(book -> {
            System.out.println(book.getBookName());
        });
        return;
    }
    @Autowired
    LoginServiceImpl loginService;
    @PostMapping("/user/login")
   public ResponseResult login(@RequestBody User user){
        System.out.println(user.getUserName()+"  "+user.getPassword());
        menuSqlRepository.selectPermsByUserId(746228272L).forEach(S->{
            System.out.println(S);
        });
     return loginService.login(user);
   }

   @GetMapping ("/logout")
   public ResponseResult logout(Long id){
       System.out.println("loginOut");

        return loginService.logout( id);
   }
   @Autowired
   MenuSqlRepository menuSqlRepository;
   @GetMapping("/query")
   @PreAuthorize("@ex.hasAuthority('cha')")
   public ResponseResult query(){
       List<String> strings = menuSqlRepository.selectPermsByUserId(746228272L);
       return new ResponseResult(200,"成功",strings);
   }

    @GetMapping("/querylist")
    @PreAuthorize("hasAnyAuthority('cha')")
    public ResponseResult querylist(){

        List<String> strings = menuSqlRepository.selectPermsByUserId(746228272L);
        return new ResponseResult(200,"成功",strings);
    }
}
